package com.jsgt.greekfire.ui.trading

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.RechargeAddressBean
import com.jsgt.greekfire.event.TradingEvent
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.activity_sale_coin.*
import org.greenrobot.eventbus.EventBus

/**
 * 我要卖币
 * author fhx
 * create time 2020/4/23 20:20
 **/
class SaleCoinActivity : BaseActivity() {
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }
    private var rechargeAddressBean: RechargeAddressBean? = null
    override fun getLayoutId(): Int = R.layout.activity_sale_coin

    override fun main() {
        title = getString(R.string.sell_coin_now)

        sale_num_et.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                val price = trading_price_et.text.toString()
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(price)) {
                    val num = s.toString().toFloat()
                    calcu_sum_tv.text = "${price.toFloat() * num * 7}"
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

        })
        trading_price_et.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                val num = sale_num_et.text.toString()
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(num)) {
                    val price = s.toString().toFloat()
                    calcu_sum_tv.text = "${price * num.toFloat() * 7}"
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

        })
        all_num_tv.setOnClickListener {
            sale_num_et.setText(rechargeAddressBean!!.assetBalance.toString())
        }
        publish_tv.setOnClickListener {
            publish()
        }
        coinViewModel.rechargeAddress("2")
        coinViewModel.rechargeAddressLiveData.observe(this, Observer {
            rechargeAddressBean = it
            sale_num_et.hint = "可用\t${rechargeAddressBean!!.assetBalance}"
        })
        coinViewModel.sellCoinLiveData.observe(this, Observer {
            ToastUtils.show(this, "提交成功")
            EventBus.getDefault().post(TradingEvent())
            finish()
        })
    }

    /**
     * 发布挂单
     */
    private fun publish() {
        val price = trading_price_et.text.toString().trim()
        val num = sale_num_et.text.toString()
        if (TextUtils.isEmpty(price)) {
            ToastUtils.show(this, "请输入交易价格")
            return
        }
        if (TextUtils.isEmpty(num)) {
            ToastUtils.show(this, "请输入数量")
            return
        }
        if (num.toFloat() > rechargeAddressBean!!.assetBalance!!.toFloat()) {
            ToastUtils.show(this, "数量有误")
            return
        }
        coinViewModel.sellCoin(num, "3", price, "", "")
    }
}